import {
  getUserInfo
} from '../api/index.js'
import {
  SET_USER_CODE,
  SET_USER_INFO,
  SET_WX_USER_INFO
} from './mutation-types.js'

export default {
  //设置用用户code
  _setUserCode({ commit }, code){
    commit(SET_USER_CODE, code);
  },
  //设置微信返回的用户信息
  _setWXUserInfo({ commit }, WXuserInfo){
    commit(SET_WX_USER_INFO, WXuserInfo);
  },
  //设置后端返回的用户信息
  async _reqUserInfo({ commit }){
    let user_token = wx.getStorageSync('userToken');
    let parmas = {};
    if(!user_token){
      wx.navigateTo({
        url: "/pages/login/main"
      })
    }else{
      parmas.user_token = user_token;
    }
    let res = await getUserInfo(parmas);
    if(res.data.level == "success"){
      commit(SET_USER_INFO, res.data.data.user_info);
    }else{
      wx.showToast({
        title: '获取用户信息失败',
        icon: 'none',
        duration: 1000
      })
    }
  },
}
